#include "lv_ui.h"

//#define DEBUG

lv_obj_t * text_date;
lv_obj_t * text_time;
lv_obj_t * text_symbol_wifi;
lv_obj_t * text_elec_value;
lv_obj_t * text_low_elec;
lv_obj_t * text_warning;

void lv_ui_main(){
  /*声明字体*/
  LV_FONT_DECLARE(lv_font_youyvan_normal_12);
  LV_FONT_DECLARE(lv_font_youyvan_normal_16);

  /*设置顶栏"类"style*/
  static lv_style_t cont_style_basic;
  lv_style_init(&cont_style_basic);
  lv_style_set_radius(&cont_style_basic,0);//无圆角
  lv_style_set_text_font(&cont_style_basic,&lv_font_youyvan_normal_12);//设置字体
  lv_style_set_border_side(&cont_style_basic,0);//无边距

  /*创建顶栏容器*/
  lv_obj_t * cont_top = lv_obj_create(lv_scr_act());
  lv_obj_add_style(cont_top,&cont_style_basic,0);//应用style
  lv_obj_set_size(cont_top,LV_HOR_RES, 20);//设置大小
  lv_obj_set_align(cont_top,LV_ALIGN_TOP_LEFT);//设置对齐
  lv_obj_clear_flag(cont_top,LV_OBJ_FLAG_SCROLLABLE);//禁止滚动
  

  /*创建主要内容容器*/
  lv_obj_t * cont_main = lv_obj_create(lv_scr_act());
  lv_obj_add_style(cont_main,&cont_style_basic,0);//应用style
  lv_obj_set_size(cont_main,LV_HOR_RES,LV_VER_RES-20);//设置大小
  lv_obj_align_to(cont_main,cont_top,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);//设置对齐
  lv_obj_clear_flag(cont_main,LV_OBJ_FLAG_SCROLLABLE);//禁止滚动
  
  /*刷新屏幕数组*/
  lv_timer_handler();

  /*在顶栏容器创建日期标签*/
  text_date = lv_label_create(cont_top);
  lv_label_set_text(text_date,"2025-03-05 星期六");
  lv_obj_set_align(text_date,LV_ALIGN_LEFT_MID);

  /*在顶栏容器创建时间标签*/
  text_time = lv_label_create(cont_top);
  lv_label_set_text(text_time,"12:40");
  lv_obj_set_align(text_time,LV_ALIGN_RIGHT_MID);

  /*添加WIFI连接状态标签(可能升级为标志组标签text_symbols)*/
  text_symbol_wifi = lv_label_create(cont_top);
  lv_obj_set_style_text_font(text_symbol_wifi, &lv_font_montserrat_12, 0);
  lv_label_set_text(text_symbol_wifi,LV_SYMBOL_WIFI);
  lv_obj_align_to(text_symbol_wifi, text_time, LV_ALIGN_OUT_LEFT_MID, -5, 0);
  lv_obj_add_flag(text_symbol_wifi,LV_OBJ_FLAG_HIDDEN);

  /*在主内容容器创建电量键标签*/
  lv_obj_t * text_elec_text = lv_label_create(cont_main);
  lv_obj_set_style_text_font(text_elec_text,&lv_font_youyvan_normal_16,0);
  lv_label_set_text(text_elec_text,"剩余电量:");
  lv_obj_align_to(text_elec_text,cont_main,LV_ALIGN_TOP_LEFT,30,0);

  /*在主内容容器创建电量值标签*/
  text_elec_value = lv_label_create(cont_main);
  lv_obj_set_style_text_font(text_elec_value,&lv_font_montserrat_28,0);
  lv_label_set_text(text_elec_value,"262.43");
  lv_obj_align_to(text_elec_value,cont_main,LV_ALIGN_TOP_MID,0,20);

  /*警告信息标签*/
  /*在主内容容器创建电量不足提示标签，默认隐藏*/
  text_low_elec = lv_label_create(cont_main);
  lv_label_set_text(text_low_elec,"剩余电量不足，请及时充值");
  lv_obj_set_align(text_low_elec,LV_ALIGN_BOTTOM_LEFT);
  lv_obj_add_flag(text_low_elec,LV_OBJ_FLAG_HIDDEN);

  /*刷新屏幕数组*/
  lv_timer_handler();

  text_warning = lv_label_create(cont_main);
  lv_label_set_text(text_warning,"网络未连接，请检查网络连接");
  if(lv_obj_has_flag(text_low_elec,LV_OBJ_FLAG_HIDDEN)==false)
  {
    lv_obj_align_to(text_warning, text_low_elec, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
  }
  else
  {
    lv_obj_set_align(text_warning,LV_ALIGN_BOTTOM_LEFT);
  }
  lv_obj_add_flag(text_warning,LV_OBJ_FLAG_HIDDEN);

  #ifdef DEBUG
  if(text_low_elec!=NULL)
  {
    Serial.println("ui init complete");
  }
  #endif // DEBUG
}

/*设置警告信息*/
void lv_ui_set_warning_text(String text)
{
  if(text!="")
  {
    lv_label_set_text(text_warning,text.c_str());
  }
}

/*更新日期信息*/
void lv_ui_set_date_text(String text)
{
  if(text!="")
  {
    lv_label_set_text(text_date,text.c_str());
  }
}

/*更新时间信息*/
void lv_ui_set_time_text(String text)
{
  if(text!="")
  {
    lv_label_set_text(text_time,text.c_str());
  }
}

/*更新电量值信息*/
void lv_ui_set_elec(String text)
{
  if(text!="")
  {
    lv_label_set_text(text_elec_value,text.c_str());
  }
}

/*设置警告信息可见性*/
void lv_ui_show_warning_text(bool Show)
{
  if(Show==true)
  {
    if(lv_obj_has_flag(text_low_elec,LV_OBJ_FLAG_HIDDEN)==false)
    {
      lv_obj_align_to(text_warning, text_low_elec, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    }
    else
    {
      lv_obj_set_align(text_warning,LV_ALIGN_BOTTOM_LEFT);
    }
    lv_obj_clear_flag(text_warning, LV_OBJ_FLAG_HIDDEN);
  }
  else
  {
    lv_obj_add_flag(text_warning, LV_OBJ_FLAG_HIDDEN);
  }
}

/*设置电量不足信息可见性*/
void lv_ui_show_low_elec_text(bool Show)
{
  if(Show==true)
  {
    lv_obj_clear_flag(text_low_elec, LV_OBJ_FLAG_HIDDEN);
  }
  else
  {
    lv_obj_add_flag(text_low_elec, LV_OBJ_FLAG_HIDDEN);
  }
}

/*设置WIFI标志可见性*/
void lv_ui_show_symbol_wifi(bool Show)
{
  if(Show==true)
  {
    lv_obj_clear_flag(text_symbol_wifi, LV_OBJ_FLAG_HIDDEN);
  }
  else
  {
    lv_obj_add_flag(text_symbol_wifi, LV_OBJ_FLAG_HIDDEN);
  }
}


